package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.AssociationDocumentQueryDTO;
import com.jeff.tianti.cms.entity.AssociationDocument;
import com.jeff.tianti.cms.service.AssociationDocumentService;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc AssociationDocumentController
 * @date 2019-07-19
 */
@Controller
@RequestMapping("/associationDocument")
public class AssociationDocumentController {

    @Autowired
    private AssociationDocumentService associationDocumentService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String title = request.getParameter("title");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        AssociationDocumentQueryDTO associationDocumentFrontQueryDTO = new AssociationDocumentQueryDTO();
        associationDocumentFrontQueryDTO.setCurrentPage(currentPage);
        associationDocumentFrontQueryDTO.setPageSize(pageSize);
        associationDocumentFrontQueryDTO.setTitle(title);
        PageModel<AssociationDocument> page = this.associationDocumentService.queryAssociationDocumentPage(associationDocumentFrontQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_ASSOCIATION_DOCUMENTS);
        model.addAttribute("associationDocumentFrontQueryDTO", associationDocumentFrontQueryDTO);

        return "/channel/AssociationDocuments/list";
    }

    @RequestMapping(value = "/stick/{id}",method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult stick(@PathVariable String id) {
        AssociationDocument associationDocument = associationDocumentService.findMaxSort();
        AssociationDocument result = associationDocumentService.find(id);
        int sort = associationDocument.getSort();
        sort++;
        result.setSort(sort);
        associationDocumentService.update(result);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        AssociationDocument associationDocument = null;
        if (StringUtils.isNotBlank(id)) {
            associationDocument = this.associationDocumentService.find(id);
        }
        model.addAttribute("associationDocument", associationDocument);
        model.addAttribute("id", id);
        return "/channel/AssociationDocuments/edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            AssociationDocument associationDocument = null;
            if (StringUtils.isNotBlank(id)) {
                associationDocument = this.associationDocumentService.find(id);
                associationDocument.setUpdateDate(new Date());
                associationDocument.setContent(content);
                associationDocument.setTitle(title);
                this.associationDocumentService.update(associationDocument);
            } else {
                associationDocument = new AssociationDocument();
                associationDocument.setCreateDate(new Date());
                associationDocument.setContent(content);
                associationDocument.setTitle(title);
                this.associationDocumentService.save(associationDocument);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            AssociationDocument associationDocument = this.associationDocumentService.find(id);
            if (associationDocument != null) {
                associationDocument.setDeleteFlag("1");
                this.associationDocumentService.update(associationDocument);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


}